package com.ruoyi.web.controller.system;


import com.ruoyi.web.service.C2CICDRunningTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/C2CICDRunningTime/")
public class C2CICDRunningTimeController {

    @Autowired
    private C2CICDRunningTimeService cicdService;

    /**
     * HTTP 接口，获取所有 versionNo 的节点名称和运行时间
     *
     * @return Map<String, Object> 包含状态、消息和数据
     */
    @GetMapping("/byAll")
    public Map<String, Object> getAllVersionDataGrouped() {
        Map<String, Object> response = new HashMap<>();

        try {
            // 调用 Service 层方法获取数据
            Map<String, List<Map<String, Object>>> data = cicdService.getRunningTimeByVersion();

            // 检查并移除 null 键
            if (data != null) {
                data.entrySet().removeIf(entry -> entry.getKey() == null || entry.getKey().isEmpty());
            }

            // 如果数据为空，返回空对象
            if (data == null || data.isEmpty()) {
                response.put("data", new HashMap<>()); // 确保 data 是 object 类型
                response.put("message", "No data available");
                response.put("status", 204);
            } else {
                response.put("data", data);
                response.put("message", "success");
                response.put("status", 200);
            }
        } catch (Exception e) {
            // 捕获异常并返回错误响应
            response.put("data", new HashMap<>()); // 确保 data 是 object 类型
            response.put("message", "Error: " + e.getMessage());
            response.put("status", 500);
        }

        return response;
    }

    /**
     * HTTP 接口，获取所有 naturalWeek 的节点名称和运行时间
     *
     * @return Map<String, Object> 包含状态、消息和数据
     */
    @GetMapping("/byWeek")
    public Map<String, Object> getNaturalWeekData() {
        Map<String, Object> response = new HashMap<>();

        try {
            // 调用 Service 层方法获取数据
            Map<String, List<Map<String, Object>>> data = cicdService.getRunningTimeByNaturalWeek();

            // 检查并移除 null 或空的 naturalWeek 键
            if (data != null) {
                data.entrySet().removeIf(entry -> entry.getKey() == null || entry.getKey().isEmpty());
            }

            // 如果数据为空，返回空对象
            if (data == null || data.isEmpty()) {
                response.put("data", new HashMap<>()); // 确保 data 是 object 类型
                response.put("message", "No data available");
                response.put("status", 204);
            } else {
                response.put("data", data);
                response.put("message", "success");
                response.put("status", 200);
            }
        } catch (Exception e) {
            // 捕获异常并返回错误响应
            response.put("data", new HashMap<>()); // 确保 data 是 object 类型
            response.put("message", "Error: " + e.getMessage());
            response.put("status", 500);
        }

        return response;
    }

}


